//
//  SWNewFeatureController.swift
//  MYSwiftWeiBo
//
//  Created by shingwai chan on 16/2/29.
//  Copyright © 2016年 ChanShingWai. All rights reserved.
//

import UIKit
import SnapKit

private let reuseIdentifier = "Cell"
//相片张数
private let itemCount = 4

class SWNewFeatureController: UICollectionViewController {

    //flowLayOut
    var flowLayOut : UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    
    //无参的构造函数
    init() {
        
        super.init(collectionViewLayout: flowLayOut)
    
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Register cell classes
        self.collectionView!.registerClass(SWCollectionCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        
        flowLayOut.itemSize = UIScreen.mainScreen().bounds.size
        flowLayOut.scrollDirection = UICollectionViewScrollDirection.Horizontal
        flowLayOut.minimumLineSpacing = 0

        collectionView?.pagingEnabled = true
        collectionView?.bounces = false

    }
    // MARK: -- UICollectionViewDataSource
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        
        return itemCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! SWCollectionCell
        
        
        cell.itemIndex = indexPath.item
        
        return cell
    }
    
    //计算偏移量 获得cell 让按钮动画
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        let index = Int(scrollView.contentOffset.x / scrollView.bounds.width )
//        print("\(index)")
        if index == itemCount - 1 {
            let indexPath = NSIndexPath(forItem: index, inSection: 0)
            let cell = collectionView?.cellForItemAtIndexPath(indexPath) as! SWCollectionCell
            cell.BtnAnimation()
        }
    }
}

//MARK: -- 自定义cell
class SWCollectionCell : UICollectionViewCell {

    var itemIndex: Int = 0 {
        didSet{
            //设置index属性的时候就设置背景图片
            backGroudView.image = (UIImage(named: "new_feature_\(itemIndex + 1)"))
            itemIndex++
            startBtn.hidden = true
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        //准备子控件
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //按钮动画
    func BtnAnimation() {
    
        startBtn.hidden = false
        startBtn.transform = CGAffineTransformMakeScale(0, 0)
        UIView.animateWithDuration(1, delay: 0.1, usingSpringWithDamping: 0.3, initialSpringVelocity: 30, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            //改变为原来的transform
            self.startBtn.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                
        }
    }
    
    //准备子控件
    func setUpUI() {
    
        contentView.addSubview(backGroudView)
        contentView.addSubview(startBtn)
        
        //约束背景
        backGroudView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(EdgeInsetsZero)
        }
        //约束按钮
        startBtn.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(contentView.snp_centerX)
            make.bottom.equalTo(contentView.snp_bottom).offset(-160)
        }
    
    }
    
    //MARK: -- 懒加载
    //背景
    lazy var backGroudView: UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    
    //开始按钮
    lazy var startBtn: UIButton = {
        
        let button = UIButton(type: UIButtonType.Custom)
        button.setTitle("开始体验", forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        button.addTarget(self, action: "startBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        button.sizeToFit()
        return button
    }()
    
    //MARK: -- 开始体验按钮点击事件 跳到MainVC
    func startBtnClick() {
        AppDelegate.outSideChoeseRootViewController(true)
    }
}



